package sharing

import base.Dimensions
import system.*

//系统初始
class InitService {
    def springSecurityService
    def initBaseService

    def init() {
        initBaseService.init()
        initQx([:])
        initBm()
        initYh()

    }

    //初始权限
    def initQx(pmap) {
      def list=[
              [authority:"SYS_Manage",parent:"",showName:"系统管理",showToJm:1,url:"",weight:2100],
              [authority:"SYS_DepartmentM",parent:"SYS_Manage",showName:"部门管理",showToJm:1,url:"/budget/mockup/system/department.gsp",weight:980],
              [authority:"SYS_RoleM",parent:"SYS_Manage",showName:"角色管理",showToJm:0,url:"/budget/role/list",weight:194],
              [authority:"SYS_AuthorityM",parent:"SYS_Manage",showName:"权限管理",showToJm:0,url:"/budget/authority/list",weight:193],
              [authority:"SYS_RoleAuthorityM",parent:"SYS_Manage",showName:"角色权限管理",showToJm:1,url:"/budget/mockup/system/roleAuthority.gsp",weight:950],
              [authority:"SYS_StaffmemberAuthorityM",parent:"SYS_Manage",showName:"用户权限管理",showToJm:1,url:"/budget/mockup/system/staffmemberAuthority.gsp",weight:930],
              [authority:"SYS_StaffmemberMenuM",parent:"SYS_Manage",showName:"用户栏目分配",showToJm:0,url:"/budget/mockup/system/staffmemberAuthorityCustomer.gsp",weight:190],
              [authority:"sys_staffmember",parent:"SYS_Manage",showName:"用户管理",showToJm:1,url:"/budget/mockup/system/personDeptmanager.gsp",weight:970],
              [authority:"SYS_roleAuthority_NoUse",parent:"SYS_Manage",showName:"角色权限分配",showToJm:1,url:"/budget/mockup/system/roleAuthority.gsp",weight:940],
              [authority:"SYS_departmentType",parent:"SYS_Manage",showName:"部门类型管理",showToJm:1,url:"/budget/departmentType",weight:990],
              [authority:"SYS_systemconfiguration",parent:"SYS_Manage",showName:"系统配置",showToJm:1,url:"/budget/mockup/system/systemconfiguration.gsp",weight:1000],

      ]
        list.each {map->
            if(map.parent&&map.parent!=""){
                map.parent=Authority.findByAuthority(map.parent)
            }
            if (map.showToJm==1){
                map.showToJm=true
            }else{
                map.showToJm=false
            }
            log.debug(map)
            saveAuthority(map)
        }

    }




    //初始单位
    def initBm() {
        //初始化 系统单位
        def topdept = "**公司"
        def department = Department.findByName(topdept)
        if (!department) {
            def deptType = DepartmentType.findByName('公司')
            if (!deptType) {
                deptType = new DepartmentType(name: '公司')
                saveClass(deptType)
            }
            department = new Department(name: topdept, code: "0105", remark: "初始化建立", departmentType: deptType, parent: Department.findByName("**公司"))
            saveClass(department)
        }
    }
    //初始用户
    def initYh() {
        // 初始化 admin 用户
        def topdept = "**公司"
        def department = Department.findByName(topdept)
        def testUser = StaffMember.findByUsername("admin" )
        if (!testUser) {
            testUser = new StaffMember(username: 'admin', realname: '系统管理员', enabled: true, password: "admin")
            testUser.department = department
            testUser.rank = Dimensions.findByCode("RY_003")
            saveClass(testUser)
        }

        //为admin 分配权限
        Authority.list().each {
            if (!StaffMemberAuthority.findByAuthorityAndStaffMember(it, testUser)) {
                StaffMemberAuthority.create(testUser, it)
            }
        }
    }


    def saveAuthority(map) {
        def adminauthority = Authority.findByAuthority(map.authority);
        if (!adminauthority) {
            adminauthority = new Authority(map); adminauthority.id = map.authority;
            adminauthority = saveComm(adminauthority).domain;
        } else {
//            if (map.forceUpdate) {
                adminauthority.properties = map
                adminauthority = saveComm(adminauthority).domain;
//            }
        }
        adminauthority
    }



    def saveComm(domain) {
        domain.clearErrors();
        def result = [success: true, error: '', domain: null];
        if (!domain.save(flush: true)) {
            domain.errors.each {
                println(it);
                result.error += it; };
            result.success = false;
        };
        result.domain = domain;
        result
    }

    def saveClass(domain) {
        domain.clearErrors()
        if (!domain.save(flush: true)) {
            domain.errors.each {
                log.error(it)
            }
        }
        domain
    }
}
